package com.seeqool.v1.dataviews;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.jfree.data.general.PieDataset;

import com.seeqool.v1.datamodel.SQLTable;

public class PieView extends BaseView implements PieDataset {
	
	private ArrayList<String>        dataKeys;
	private Hashtable<String, Float> dataValues;
	private String					 title;

	public PieView(SQLTable table, int majorAxis, int minorAxis) {
		dataKeys   = new ArrayList<String>();
		dataValues = new Hashtable<String, Float>();
		float[] dataY = table.getFloats(minorAxis);
		for (int row=0; row<table.getRowCount(); row++) {
			String key=table.getValueAt(row, majorAxis).toString();
			if (dataValues.contains(key)) {
				dataValues.put(key, dataValues.get(key)+dataY[row]);
			} else {
				dataKeys.add(key);
				dataValues.put(key, dataY[row]);				
			}
		}
		title = table.getColumnName(minorAxis);
	}
	
	public String getTitle() { return title; }

	@SuppressWarnings("unchecked")
	public int getIndex(Comparable key)         { return dataKeys.indexOf(key); }
	public Comparable<String> getKey(int index) { return dataKeys.get(index); }

	public List<String> getKeys()               { return dataKeys; }
	@SuppressWarnings("unchecked")
	public Number getValue(Comparable key)      { return dataValues.get(key); }
	public int getItemCount()                   { return dataKeys.size(); }
	public Number getValue(int index)           { return dataValues.get(dataKeys.get(index)); }

}
